package com.soft.knowledge.controller;

import com.soft.knowledge.req.DocQueryReq;
import com.soft.knowledge.req.DocSaveReq;
import com.soft.knowledge.req.VoteReq;
import com.soft.knowledge.resp.CommonResp;
import com.soft.knowledge.resp.DocQueryResp;
import com.soft.knowledge.resp.PageResp;
import com.soft.knowledge.service.DocService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * 电子书(Doc)表控制层
 *
 *  @author makejava
 *  @since 2022-11-19 13:17:14
 */
@RestController
@RequestMapping("doc")
public class DocumentController {
    /**
     * 服务对象
     */
    @Resource
    private DocService docService;

    /**
     * 通过请求参数列表name属性进行模糊查询
     *
     * @param req 请求参数列表
     * @return 多条数据
     */
    @GetMapping("/list")
    public CommonResp list(@Valid DocQueryReq req){
        CommonResp<PageResp<DocQueryResp>> resp = new CommonResp<>();
        PageResp<DocQueryResp> list = docService.list(req);
        resp.setContent(list);
        resp.setMessage("查询成功！");
        return resp;
    }

    @GetMapping("/all/{ebookId}")
    public CommonResp all(@PathVariable Long ebookId){
        CommonResp<List<DocQueryResp>> resp = new CommonResp<>();
        List<DocQueryResp> list = docService.all(ebookId);
        resp.setContent(list);
        resp.setMessage("查询成功！");
        return resp;
    }
    @PostMapping("/save")
    public CommonResp save(@RequestBody @Valid DocSaveReq req){//post json方式请求要加@RequestBody，form表单方式提交不需要加该注解
        CommonResp resp = new CommonResp<>();
        docService.save(req);
        resp.setMessage("保存成功");
        return resp;
    }
    //restful
    @RequestMapping(value = "/delete/{idsStr}", method = RequestMethod.DELETE)
    public CommonResp delete(@PathVariable String idsStr){
        CommonResp resp = new CommonResp<>();
        List<String> list = Arrays.asList(idsStr.split(","));
        docService.delete(list);
        resp.setMessage("删除成功");
        return resp;
    }
    @GetMapping(value = "/find-content/{id}")
    public CommonResp findContent(@PathVariable Long id) {
        CommonResp<String> resp = new CommonResp<>();
        resp.setContent(docService.findContent(id));
        return resp;
    }

    @PostMapping("/vote")
    public CommonResp vote(@RequestBody @Valid VoteReq req) {
        CommonResp resp = new CommonResp();
        docService.vote(req);
        return resp;
    }
}
